package com.soriya.nestlive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.entity.CategoryEntity;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.param.CategoryListParam;
import com.soriya.nestlive.service.CategoryService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    private final CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size, CategoryListParam param) {
        Page<CategoryEntity> page = new Page<>(current, size);
        PageResult<CategoryEntity> result = categoryService.pageQuery(page, param);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<CategoryEntity> getById(@PathVariable Long id) {
        CategoryEntity data = categoryService.getById(id);
        return Result.success(data);
    }

    @PostMapping
    public Result<?> save(@RequestBody CategoryEntity category) {
        boolean save = categoryService.save(category);
        return Result.update(save);
    }

    @PutMapping
    public Result<?> updateById(@RequestBody CategoryEntity category) {
        boolean b = categoryService.updateById(category);
        return Result.update(b);
    }

    @DeleteMapping("/{id}")
    public Result<?> removeById(@PathVariable Long id) {
        boolean b = categoryService.removeById(id);
        return Result.update(b);
    }

}
